package com.GroupSix.service;

import com.GroupSix.dao.PaperGenerationRuleDao;
import com.GroupSix.entity.PaperGenerationRule;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service("PaperGenerationRuleService")
public class PaperGenerationRuleServiceImpl extends ServiceImpl<PaperGenerationRuleDao, PaperGenerationRule>
        implements PaperGenerationRuleService {

    //增
    @Override
    public Integer add(PaperGenerationRule rule) {
        rule.setCreateTime(LocalDateTime.now());
        rule.setUpdateTime(LocalDateTime.now());
        return baseMapper.insert(rule);
    }
    //删
    @Override
    public Integer delete(int id) {
        return baseMapper.deleteById(id);
    }
    //查
    @Override
    public PaperGenerationRule find(int id) {
        return baseMapper.selectById(id);
    }
    //改
    @Override
    public Integer update(PaperGenerationRule rule) {
        rule.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(rule);
    }
    //分页查询
    @Override
    public IPage<PaperGenerationRule> list(PaperGenerationRule rule, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<PaperGenerationRule> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PaperGenerationRule::getId, rule.getId());

        int currentPage = 1;
        int limit = 10;
        if (pageNo != null && pageNo > 0) {
            currentPage = pageNo;
        }
        if (pageSize != null && pageSize > 0) {
            limit = pageSize;
        }

        IPage<PaperGenerationRule> page = Page.of(currentPage, limit);

        return baseMapper.selectPage(page, wrapper);
    }
}
